package com.htic.suricata.spreadsheet.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.htic.arq.core.util.StringUtils;
import com.htic.suricata.core.model.TestSourceParameters;
import com.htic.suricata.core.util.SuricataConstants;

public class TestSourceSpreadSheetParameters extends TestSourceParameters {

	private String filePath;

	@Deprecated private int fileFormat;


	public TestSourceSpreadSheetParameters(String pathParamFile) {
		super(pathParamFile);
	}


	//API
	@Override
	public void loadConfigParameters(String pathParamFile) {
		InputStream is				= null;
		Properties configParameters	= null;

		try {
		  is = new FileInputStream(new File(pathParamFile));
		  configParameters = new Properties();
		  configParameters.load(is);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException ();
		} finally {
			if (is!=null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		loadRequiredConfigParameters(configParameters);
		loadOptionalConfigParameters(configParameters);
	}


	private void loadRequiredConfigParameters(Properties configParameters) {
		String propKey;
		String propValue;
		String appName	= StringUtils.removeChar(SuricataConstants.APP_NAME.toLowerCase(), ' ');
		String modName	= StringUtils.removeChar(SuricataConstants.MODULE_NAME.toLowerCase(), ' ');

		propKey		= appName.concat(".").concat(SuricataConstants.PROPNAME_TESTSOURCE).concat(".").concat(modName).concat(".").concat(SuricataConstants.PROPNAME_FILEPATH);
		propValue	= configParameters.getProperty(propKey);

		if (propValue == null) {
			throw new TestSourceSpreadSheetException(TestSourceSpreadSheetException.TESTPARAMETERS_MISSING_PARAM_REQUIRED);
		} else {
			this.setFilePath(propValue);
		}
	}

	private void loadOptionalConfigParameters(Properties configParameters) {
		String propKey;
		String propValue;
		String appName	= StringUtils.removeChar(SuricataConstants.APP_NAME.toLowerCase(), ' ');
		String modName	= StringUtils.removeChar(SuricataConstants.MODULE_NAME.toLowerCase(), ' ');

		propKey		= appName.concat(".").concat(SuricataConstants.PROPNAME_TESTSOURCE).concat(".").concat(modName).concat(".").concat(SuricataConstants.PROPNAME_FILEFORMAT);
		propValue	= configParameters.getProperty(propKey);
		if (propValue != null) {
			try {
				this.setFileFormat(new Integer(propValue).intValue());
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
	}


	//Private Getters && Setters
	private void setFilePath(String pathFile) {
		this.filePath = pathFile;
	}
	private void setFileFormat(int fileFormat) {
		this.fileFormat = fileFormat;
	}

	//Getters && Setters
	public String getFilePath() {
		return filePath;
	}
	@Deprecated public int getFileFormat() {
		return fileFormat;
	}
}